﻿using System;
using System.Data;
using System.IO;
using NUnit.Framework;
using Smartkernel.Data;
using Smartkernel.Extensions;
using System.Text;

namespace Smartkernel.NUnitTest.Extensions
{
    [Category("Extensions")]
    [TestFixture]
    public class SmartDataTableExtensionTest
    {
        [Test]
        public void Test()
        {
            //【01】聚合计算（涉及方法：Max、Min、Sum、Avg）
            string sqlConnectionString = Smartkernel.SmartCoreEntity.Instance.ConnectionString;
            string sentence = "SELECT TABLE_NAME AS TableName,COLUMN_NAME AS ColumnName,CHARACTER_MAXIMUM_LENGTH AS ColumnLength FROM INFORMATION_SCHEMA.COLUMNS";
            DataTable dataTable = new DataTable();
            SmartDatabase.Fill(sqlConnectionString, sentence, dataTable);
            Assert.AreEqual(dataTable.Max("ColumnLength") > 0, true);
            Assert.AreEqual(dataTable.Min("ColumnLength"), -1);
            Assert.AreEqual(dataTable.Sum("ColumnLength") > 0, true);
            Assert.AreEqual(dataTable.Avg("ColumnLength") > 0, true);

            //【02】保存为CSV文件（涉及方法：SaveToCSV）
            string path = Environment.CurrentDirectory + @"\" + DateTime.Now.Ticks.ToString() + ".csv";
            dataTable.SaveToCSV(path);
            File.Delete(path);
        }
    }
}
